﻿<?php
abstract class Uuch_Reader_ProductReader{
	
	/** 
	 * 根据URL返回商品信息
	 * @param string $url
	 * @return Application_Model_Product
	 */
	public abstract function getInfo($url);
	
        /**
         * 根据URL返回一个商品信息解析器
         * @param string $url
         * @return Application_Model_Reader_ProductReader 
         */
	public static function Create($url){
            if(empty($url)) return;
            $uri = parse_url($url);
            $name = explode('.',$uri['host']);
            
            //网站与采集器对应关系
            $webReaderMap = array('Tmall'=>'Taobao','360buy'=>'Jd','Dangdang'=>'Common','1688'=>'Common','Jd'=>'Jd','Yhd'=>'Yhd','Yixun'=>'Yixun','Suning'=>'Suning','Amazon'=>'Amazon', '95095'=>'Taobao');
            
            $reader = ucfirst(strtolower($name[count($name)-2]));
            
            if(isset($webReaderMap[$reader]))
                $reader = $webReaderMap[$reader];
            
            //require_once APPLICATION_PATH. '/models/readers/'.$reader.'Reader.php';
            $reader = 'Uuch_Reader_'.$reader.'Reader';
            
            return new $reader();
	}
        
    /**
        *返回URL中的QueryString的值
        * @param type $url
        * @param type $name
        * @return string 
        */
    protected function query($url,$name){
        $uri = parse_url($url);
        $query = explode('&', $uri['query']);
        $map = array();
        foreach ($query as $key) {
            $str = explode('=', $key);
            if (count($str)>1)
                $map[$str[0]] = $str[1];
            else
                $map[$str[0]] = '';
        }
        if(array_key_exists($name, $map))
            return $map[$name];
        else
            return NULL;
    }
}
